package com.wlhse.util;

import com.wlhse.dao.CompanyDao;
import com.wlhse.dao.TaskDao;
import com.wlhse.dto.inDto.TaskManagementDto;
import com.wlhse.dto.outDto.PlanOutDto;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class GetNamesUtil {

    @Resource
    private TaskDao taskDao;

    @Resource
    private CompanyDao companyDao;

    public List<TaskManagementDto> changeEmployeeCodeToNames(List<TaskManagementDto> list) {

        for (TaskManagementDto t : list) {
            StringBuilder leader = new StringBuilder();
            int flag = 0;
            for (String id : t.getLeaderIDs().split(",")) {
                flag++;
                if (flag < t.getLeaderIDs().split(",").length) {
                    leader.append(taskDao.getPersonName(Integer.parseInt(id))).append(",");
                } else {
                    leader.append(taskDao.getPersonName(Integer.parseInt(id)));
                }
            }
            t.setLeader(leader.toString());
        }
        return list;
    }

    public List<PlanOutDto> changeCompanyCodeToNames(List<PlanOutDto> list) {
        for (PlanOutDto plan : list) {
            String names = changeCompanyCodeToNamesSingle(plan.getCompanyCode());
            plan.setCompanyName(names);
        }
        return list;
    }

    public String changeCompanyCodeToNamesSingle(String codes) {
            StringBuilder names = new StringBuilder();
            int flag = 0;
            if (StringUtils.isNotBlank(codes)) {
                for (String com : codes.split(",")) {
                    flag++;
                    if (flag < codes.split(",").length) {
                        names.append(companyDao.queryByCompanyCode(com)).append(",");
                    } else {
                        names.append(companyDao.queryByCompanyCode(com));
                    }
                }
            }
        return names.toString();
    }
}
